<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-10 23:59:20 +0200 (Thu, 10 Jul 2008) $
 * $Rev: 1285 $
 * $Author: gavinspearhead $
 * $Id: parsenzb.php 1285 2008-07-10 21:59:20Z gavinspearhead $
 */
if (!defined ('ORIGINAL_PAGE'))
	define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathpn = realpath(dirname(__FILE__));

require_once "$pathpn/../functions/html_includes.php";


verify_challenge($_POST['challenge']);

$rprefs = load_config($db);
$postid = get_request('postid', '');
$uc = new urdd_client($rprefs['urdd_host'], $rprefs['urdd_port'], $userID);

if (!$uc->is_connected())
	throw new exception($LN['error_urddconnect']);


function get_post_vals(DatabaseConnection $db, $userID, &$timestamp)
{
 global $LN;
    $dlpath = realpath(get_dlpath($db) . POST_PATH);
    $groupID = post_form('groupid', '');
    if ($groupID == '' || !is_numeric($groupID) || !group_exists($db, $groupID))
        throw new exception ("Group does not exist $groupID"); 

    $src_dir = realpath(post_form('directory', ''));
    if ($src_dir == '' || (substr($src_dir, 0, strlen($dlpath)) != $dlpath))
        throw new exception ("Invalid directory $src_dir"); 

    $tmp_dir = '';
    $subject = trim(post_form('subject', ''));
    if ($subject == '')
        throw new exception ('Subject missing'); 

    $poster_id = trim(post_form('posteremail', ''));
    if ($poster_id == '')
        throw new exception ('Poster email missing'); 
    $poster_name = trim(post_form('postername', ''));
    if ($poster_name == '')
        throw new exception ('Poster name missing'); 

    $recovery_par = post_form('recovery', NULL);
    if ($recovery_par === NULL || !is_numeric($recovery_par))
        throw new exception ('Invalid recovery size'); 
    $filesize_rar = post_form('filesize', NULL);
    if ($filesize_rar === NULL || !is_numeric($filesize_rar))
        throw new exception ('Invalid rar file size'); 
    $delete_files = post_form('delete_files', NULL);
    $delete_files = ($delete_files !== NULL) ? 1: 0;
    $status = POST_READY;
    $timestamp = trim(post_form('timestamp', NULL));
    $now = time();
    $size = dirsize($src_dir);
    if ($timestamp != NULL && $timestamp != '') {
        $time_int = strtotime($timestamp);
		if ($time_int === FALSE) {
            $timestamp = NULL;
			near_fatal_error($LN['browse_invalid_timestamp'], 'timestamp', NULL);
        } else {
            if ($time_int < $now) { // the time is before now, so probably means tomorrow 
                $time_int += 24 * 60 * 60; // next day
                $timestamp .= ' +1 day';
            }
            $now = $time_int;
        }
    } else
        $timestamp = NULL;

    $vals = array('userID'=> $userID,'groupID'=> $groupID,'src_dir'=>  $src_dir,'tmp_dir'=>  $tmp_dir, 'subject'=> $subject,'poster_id'=>  $poster_id,'poster_name'=>  $poster_name, 'recovery_par'=> $recovery_par, 'filesize_rar'=> $filesize_rar,'delete_files'=>  $delete_files,'status'=> $status, 'start_time'=> $now, 'size'=>$size);
    return $vals;
}


function add_post_to_db(DatabaseConnection $db, $userID, &$timestamp)
{
    $cols = array('userid', 'groupid', 'src_dir', 'tmp_dir', 'subject', 'poster_id', 'poster_name', 'recovery_par', 'filesize_rar', 'delete_files', 'status', 'start_time', 'size');
    $vals = get_post_vals( $db, $userID, $timestamp);

    $db->insert_query('postinfo', $cols, $vals);
    $id = $db->get_last_id();
    $stat_id = add_stat_data($db, STAT_ACTION_POST, 0, $userID);
    set_stat_id($db, $id, $stat_id, TRUE);
    return $id;
}


function update_post_db($db, $userID, $postid, &$timestamp)
{
    $cols = array('userid', 'groupid', 'src_dir', 'tmp_dir', 'subject', 'poster_id', 'poster_name', 'recovery_par', 'filesize_rar', 'delete_files', 'status', 'start_time', 'size');
    $vals = get_post_vals( $db, $userID, $timestamp);
    $db->escape($postid, TRUE);
    $db->update_query('postinfo', $cols, $vals, "\"id\" = $postid");
}

$timestamp= NULL;
if ($postid == '') { // we got a new post
    $id = add_post_to_db($db, $userID, $timestamp);
    $uc->post($id);
    if ($timestamp === NULL)
        $uc->unpause(get_command(COMMAND_POST) . " $id");
    else 
        $uc->schedule(get_command(COMMAND_CONTINUE), '"' . get_command(COMMAND_POST) . " $id\"", $timestamp);
} else { // we need to update a post
    $uc->unschedule(get_command(COMMAND_CONTINUE), '"' . get_command(COMMAND_POST) . " $postid\"");
    update_post_db($db, $userID, $postid, $timestamp);
    if ($timestamp === NULL)
        $uc->unpause(get_command(COMMAND_POST) . " $postid");
    else 
        $uc->schedule(get_command(COMMAND_CONTINUE), '"' . get_command(COMMAND_POST) . " $postid\"", $timestamp);
}
redirect('transfers.php');
?>
